package com.skyline.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.exception.OperateFailedException;
import com.skyline.base.type.Activity;
import com.skyline.common.bean.Page;
import com.skyline.user.dao.FanDao;
import com.skyline.user.model.Fan;
import com.skyline.user.service.FanService;
import com.skyline.user.type.IdolType;

@Service("fanService")
public class FanServiceImpl implements FanService {

	@Autowired
	private FanDao fanDao;

	@Override
	public void follow(Long idolId, String idolNickname, String idolPortrait, Long fanId, String fanNickname,
			String fanPortrait, IdolType idolType) {
		if (idolId == null || fanId == null || idolId == 0L || fanId == 0L) {
			throw new OperateFailedException("用户" + fanId + "关注" + idolId + "失败");
		}
		int count = fanDao.countFans(idolId, fanId, Activity.DELETED);
		if (count != 0) {
			fanDao.updateFanActivity(idolId, fanId, Activity.NORMAL);
		} else {
			fanDao.insertFan(idolId, idolNickname, idolPortrait, fanId, fanNickname, fanPortrait, idolType);
		}
		// if (idolType == IdolType.USER) {
		// if (fanDao.existAttention(fanId, idolId, Activity.NORMAL)) {
		// fanDao.updateAttentionType(idolId, fanId, true);
		// fanDao.updateAttentionType(fanId, idolId, true);
		// }
		// }

	}

	@Override
	public List<Fan> listIdols(Long fanId, IdolType idolType, Page page) {
		if (fanId == null || fanId == 0L) {
			return new ArrayList<Fan>(0);
		}
		if (idolType == null) {
			return fanDao.queryIdolsByFanId(fanId, page, Activity.NORMAL);
		} else {
			return fanDao.queryIdolsByFanIdAndIdolType(fanId, idolType, page, Activity.NORMAL);
		}
	}

	@Override
	public List<Fan> listIdols(Long fanId, IdolType idolType) {
		if (fanId == null || fanId == 0L) {
			return new ArrayList<Fan>(0);
		}
		return listIdols(fanId, idolType, null);
	}

	@Override
	public List<Fan> listFans(Long idolId, Page page) {
		if (idolId == null || idolId == 0L) {
			return new ArrayList<Fan>(0);
		}
		return fanDao.queryFansByIdolId(idolId, page, Activity.NORMAL);
	}

	@Override
	public int countFans(Long idolId) {
		if (idolId == null || idolId == 0L) {
			return 0;
		}
		return fanDao.countNormalFans(idolId);
	}

	@Override
	public int countIdols(Long fanId, IdolType type) {
		if (fanId == null || fanId == 0L) {
			return 0;
		}
		return fanDao.countNormalIdols(fanId, type);
	}

	@Override
	public boolean isFan(Long idolId, Long fanId) {
		if (idolId == null || fanId == null || idolId == 0L || fanId == 0L) {
			return false;
		}
		return (fanDao.countFans(idolId, fanId, Activity.NORMAL) > 0);
	}

	@Override
	public boolean isFanEachOther(Long idolId, Long fanId) {
		if (idolId == null || fanId == null || idolId == 0L || fanId == 0L) {
			return false;
		}
		return isFan(idolId, fanId) && isFan(fanId, idolId);
	}

	@Override
	public void unFollow(Long idolId, Long fanId) {
		if (idolId == null || fanId == null || idolId == 0L || fanId == 0L) {
			throw new OperateFailedException("用户" + fanId + "取消关注" + idolId + "失败");
		}
		if (isFan(idolId, fanId)) {
			fanDao.updateFanActivity(idolId, fanId, Activity.DELETED);
		}
		// fanDao.updateAttentionType(idolId, fanId, false);
		// fanDao.updateAttentionType(fanId, idolId, false);

	}

	@Override
	public List<Fan> listBothIdols(Long ownerId, Long viewerId, IdolType idolType, Page page) {
		if (ownerId == null || viewerId == null || ownerId == 0L || viewerId == 0L) {
			return new ArrayList<Fan>(0);
		}
		if (idolType == null) {
			return fanDao.queryBothIdols(ownerId, viewerId, page, Activity.NORMAL);
		} else {
			return fanDao.queryBothIdolsByIdolType(ownerId, viewerId, idolType, page, Activity.NORMAL);
		}
	}

	@Override
	public List<Fan> listIdolsWithFollowed(Long ownerId, Long viewerId, IdolType idolType, Page page) {
		if (ownerId == null || ownerId == 0L) {
			return new ArrayList<Fan>(0);
		} else if (viewerId == null || viewerId == 0L) {
			return listIdols(ownerId, idolType, page);
		}
		if (idolType == null) {
			return fanDao.queryIdolsWithFollowed(ownerId, viewerId, page, Activity.NORMAL);
		} else {
			return fanDao.queryIdolsByIdolTypeWithFollowed(ownerId, viewerId, idolType, page, Activity.NORMAL);
		}
	}

	@Override
	public List<Fan> listFansWithFollowed(Long idolId, Long viewerId, Page page) {
		if (idolId == null || idolId == 0L) {
			return new ArrayList<Fan>(0);
		} else if (viewerId == null || viewerId == 0L) {
			return listFans(idolId, page);
		} else {
			return fanDao.queryFansWithFollowed(idolId, viewerId, page, Activity.NORMAL);
		}
	}

	@Override
	public List<Fan> listEachOtherIdols(Long fanId, Page page) {
		if (fanId == null || fanId == 0L) {
			return new ArrayList<Fan>(0);
		}
		return fanDao.queryEachOtherIdols(fanId, page, Activity.NORMAL);
	}
}